import {actionTypes} from './index'
import http from '@/http'
import * as constant from '@/config/constant'
import history from '@/history'

const resetModalStatus = () => ({
    type: actionTypes.RESET_MODAL_STATUS,
    value: {
        visible: false,
        confirmLoading: false,
        modalText: constant.OPERATION_CALL_REBOOT,
        modalTitle: constant.PLEASE_CHECK,
        progressVisible: false,
        progressPercent: 0,
        progressAction: 'active'
    }
})
const updateData = (value) => ({
    type: actionTypes.UPDATE_DATA,
    value
})
const changeProgressPercent = () => ({
    type: actionTypes.CHANGE_PROGRESS_PERCENT
})
export const requestList = () => {
    return (dispatch) => {
        http.get('/basicSet/interfaceInit').then((res) => {
            if (res.status === 1) {
                dispatch(updateData(res.formData))
            }
        })
    }
}


export const interfaceSet = (wanCount) => {
    return (dispatch) => {
        http.post( 'basicSet/interfaceSet',{ wanCount },false).then((res) => {
            if (res.status === 1) {
                const value = {
                    modalText: constant.OPERATE_SUCCESS_REBOOT,
                    progressVisible: true,
                }
                dispatch(changeModalStatus(value))
                setInterval(() => {
                    dispatch(changeProgressPercent())
                }, 1000*(120/100))
                setTimeout(() => {
                    const value = {
                        modalText:constant.REFRESH,
                        progressActive:'success'
                    }
                    dispatch(changeModalStatus(value))
                }, 1000 * 120)
                setTimeout(() => {
                    // window.location.href = '/'
                    history.myPush({
                        url:'/home',
                        key:'home'
                    })
                    dispatch(resetModalStatus())
                }, 1000 * 122)
            } else {
                dispatch(changeModalStatus({modalText: constant.OPERATE_SUCCESS_REBOOT}))
            }
        })
    }
}

export const changeModalStatus = (value) => ({
    type: actionTypes.CHANGE_MODAL_STATUS,
    value
})
